let commonDialog = {}

commonDialog.alert = function (title, detail, ok) {
  let dialogId = Math.random() * 100
  let dialogHtml = `<div class="dialog-box">
                        <div class="dialog-main">
                            <div class="dialog-title">${title}</div>
                            <div class="dialog-detail">${detail}</div>
                            <div class="dialog-ctr">
                                <div id="sure_${dialogId}" class="sureBtn">确定</div>
                            </div>
                        </div>
                     </div>`
  let cover = document.createElement('div')
  document.body.appendChild(cover)
  cover.innerHTML = dialogHtml
  document.getElementById('sure_' + dialogId).addEventListener('click', function () {
    if (typeof (ok) === 'function') {
      ok()
    }
    cover.parentNode.removeChild(cover)
    cover.innerHTML = null
  })
}

commonDialog.confirm = function (title, detail, ok, cancle) {
  let dialogId = Math.random() * 100
  let dialogHtml = `<div class="dialog-box">
                          <div class="dialog-main">
                              <div class="dialog-title">${title}</div>
                              <div class="dialog-detail">${detail}</div>
                              <div class="dialog-ctr">
                                  <div id="cancle_${dialogId}" class="cancleBtn">取消</div>
                                  <div id="sure_${dialogId}" class="sureBtn">确定</div>
                              </div>
                          </div>
                       </div>`
  let cover = document.createElement('div')
  document.body.appendChild(cover)
  cover.innerHTML = dialogHtml
  document.getElementById('sure_' + dialogId).addEventListener('click', function () {
    if (typeof (ok) === 'function') {
      ok()
    }
    cover.parentNode.removeChild(cover)
    cover.innerHTML = null
  })
  document.getElementById('cancle_' + dialogId).addEventListener('click', function () {
    if (typeof (cancle) === 'function') {
      cancle()
    }
    cover.parentNode.removeChild(cover)
    cover.innerHTML = null
  })
}

export default commonDialog
